package inabit.data;

import inabit.base.WorkingMemory;
import inabit.data.expression.ConsequentExpression;
import inabit.data.expression.Expression;

import java.util.List;
/**A simpler form of a rule that can be fired and stores its fired state,
 * to fire a rule is to make its consequent true.
 * @author Rafael Rocha*/
public class FireableRule extends Rule{
	private boolean fired;
	
	public FireableRule(String id, Expression precedent, ConsequentExpression consequent) {
		super(id, precedent, consequent);
	}
	/**reset the fired state of this rule so it can be fired again*/
	public void reset() {
		fired = false;
	}
	/**Returns all variables referred at the consequent of this rule and that, therefore, can
	 * be changed when the rule is fired.*/
	public List<Variable> getTargetVariables(WorkingMemory wm) {
		return ((ConsequentExpression)getConsequent()).getTargetVariables(wm);
	}
	/**To fire a rule is to execute its consequent expression, the truth value
	 * specifies the intensity of true.<br>
	 * <br>
	 * At a crisp environment truth value must be
	 * either 0 or 1. However, it can support multi-valued logic as fuzzy logic, 
	 * by changing the truth value between these values and using it properly
	 * at the consequent expression fire method.*/
	public void fire(WorkingMemory wm, double truth) throws Exception {
		((ConsequentExpression)getConsequent()).fire(wm, truth);
		fired = true;
	}
	public boolean hasFired() {
		return fired;
	}
}
